package com.blog.dto;

import com.blog.utils.IKAnalyzerUtils;
import com.blog.utils.PinyinUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;

import org.springframework.data.elasticsearch.annotations.CompletionField;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.suggest.Completion;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EsArticleDTO {
    /**
     * 主键
     */
    @Id
    private Integer id;
    /**
     * 提示词
     * */
    @CompletionField(maxInputLength = 100)
    private Completion suggest;
    /**
     * 博客标题
     */

    private String title;

    /**
     * 博客正文，markdown语法
     */

    private String content;

    /**
     * 创建时间
     */
    @Field(type = FieldType.Date, format = {}, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Field(type = FieldType.Date, format = {}, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;

    /**
     * 浏览量
     */
    private Integer viewCount;

    /**
     * 分类id
     */
    private Integer categoryId;

    /**
     * 0:禁用,1:置顶,2:正常
     */
    private Integer status;

    /**
     * 博客创建作者
     */
    private String author;

    /**
     * 创建者id
     */
    private Integer authorId;
    public void initSuggest() {
        List<String> keywords = IKAnalyzerUtils.split(title, true);
//        keywords.add(title);

        Set<String> distinctSuggestions = new LinkedHashSet<>();
        for (String keyword : keywords) {
            if (keyword.trim().isEmpty()) continue;
            distinctSuggestions.add(keyword);
        }

        // 过滤短词并创建Completion对象
        List<String> validSuggestions = distinctSuggestions.stream()
                .filter(s -> s.length() > 1)
                .collect(Collectors.toList());

        this.suggest = new Completion(validSuggestions.toArray(new String[0]));
    }

}
